/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cloudfactory.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alibaba.druid.support.json.JSONUtils;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.MD5EncryptionUtil;
import com.thinkgem.jeesite.modules.cloudfactory.dao.CloudfactoryUserDao;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUser;

/**
 * 单表生成Service
 * @author ThinkGem
 * @version 2018-09-02
 */
@Service
@Transactional
public class CloudfactoryUserService extends CrudService<CloudfactoryUserDao, CloudfactoryUser> {
	
	@Autowired
	private CloudfactoryUserDao cloudfactoryUserDao;
	public CloudfactoryUser get(String id) {
		return super.get(id);
	}
	
	public List<CloudfactoryUser> findList(CloudfactoryUser cloudfactoryUser) {
		return super.findList(cloudfactoryUser);
	}
	
	public Page<CloudfactoryUser> findPage(Page<CloudfactoryUser> page, CloudfactoryUser cloudfactoryUser) {
		return super.findPage(page, cloudfactoryUser);
	}
	
	@Transactional(readOnly = false)
	public void save(CloudfactoryUser cloudfactoryUser) {
		super.save(cloudfactoryUser);
	}
	
	@Transactional(readOnly = false)
	public void delete(CloudfactoryUser cloudfactoryUser) {
		super.delete(cloudfactoryUser);
	}
	/**
	 * 
	* @Title: saveRegister
	* @Description: 保存注册信息
	* @param @param request
	* @param @param model
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	public String saveRegister(HttpServletRequest request, Model model) {
		String name = request.getParameter("name");
		String psw = request.getParameter("psw");
		String phone = request.getParameter("phone");
		String mail = request.getParameter("mail");
		System.out.println("");
		CloudfactoryUser cloudfactoryUser = new CloudfactoryUser();
		cloudfactoryUser.setLoginName(name);
		if(super.findList(cloudfactoryUser).size()>0) {
			return "账号名重复，请重新命名";
		}
		cloudfactoryUser.setLoginPassword(MD5EncryptionUtil.getEncryption(psw));
		cloudfactoryUser.setMobile(phone);
		cloudfactoryUser.setEmail(mail);
		cloudfactoryUser.setCreateDate(new Date());
		cloudfactoryUser.setIsApprove("3");
		super.save(cloudfactoryUser);
		cloudfactoryUser = new CloudfactoryUser();
		cloudfactoryUser.setLoginName(name);
		List<CloudfactoryUser> cloudfactoryUsers = super.findList(cloudfactoryUser);
		Map<String,Object> map = new HashMap<String,Object>();
		if(cloudfactoryUsers.size()>0) {
			model.addAttribute("cloudfactoryUser",cloudfactoryUsers.get(0));
			model.addAttribute("isLogin",1);
			
			map.put("success", "success");
			map.put("id", cloudfactoryUsers.get(0).getId());
			return JSONUtils.toJSONString(map);	
		} else {
			return JSONUtils.toJSONString(map.put("success","注册失败，请重新注册"));
		}
		
		
	}
	/**
	 * @return 
	 * 
	* @Title: audit
	* @Description: 审核
	* @param @param cloudfactoryUser    参数
	* @return void    返回类型
	* @throws
	 */
	public int audit(CloudfactoryUser cloudfactoryUser) {
		cloudfactoryUser.setIsApprove("2");
		return cloudfactoryUserDao.update(cloudfactoryUser);
	}
	
}